//
//  SimpleRegistVC.m
//  qunfa
//
//  Created by Admin on 2017/8/15.
//  Copyright © 2017年 cuixiaocun. All rights reserved.
//

#import "SimpleRegistVC.h"

@interface SimpleRegistVC ()<UITextFieldDelegate,SRActionSheetDelegate,CXCNavgationDelegate>
{
    //底部scrollview
    TPKeyboardAvoidingScrollView *bgScrollView;
    NSString *levelString;
    NSString *memberlat;
    NSString *memberlng;
    NSString *photoString;
    NSInteger listTime;
    NSTimer *huoquTimer;
    UIButton *sendCodeBtn;
    //下一步按钮
    UIButton*nextBtn;
    UIImageView *imgV;
    
}


@end

@implementation SimpleRegistVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    NavgationView *nav =[[NavgationView alloc]initWithFrame:CGRectMake(0, 0, CXCWidth, 64) with:@"会员登录" isBackBtn:@"YES" rightBtnName:@"NO"];
    [self.view addSubview:nav];
    nav.delegate =self;
    nav.navLabel.text =@"会员注册";
    self.view.backgroundColor =BGColor;
    [self mainView];
}
- (void)retunButtonAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)mainView
{
    bgScrollView =[[TPKeyboardAvoidingScrollView alloc] initWithFrame:CGRectMake(0, 64,CXCWidth, CXCHeight-64 )];
    [bgScrollView setUserInteractionEnabled:YES];
    [bgScrollView setBackgroundColor:BGColor];
    [self.view addSubview:bgScrollView];
    [bgScrollView setContentSize:CGSizeMake(CXCWidth, 1610*Width)];
    NSArray*leftArr =@[@"账号",@"密码",@"确认密码"] ;
    NSArray *rightArr =@[@"请输入手机号码注册",@"6-16位数字、字母或字符",@"确认密码"];
    //列表
    for (int i=0; i<3; i++) {
        UIView *bgview =[[UIView alloc]init];
        bgview.backgroundColor =[UIColor whiteColor];
        [bgScrollView addSubview:bgview];
        
        UILabel* labe = [[UILabel alloc]initWithFrame:CGRectMake(32*Width, 0,200*Width , 106*Width)];
        labe.text = leftArr[i];
        labe.font = [UIFont systemFontOfSize:15];
        labe.textColor = [UIColor grayColor];
        [bgview addSubview:labe];
            UITextField *inputText = [[UITextField alloc] init];
            [inputText setTag:i+10];
            if (i==1||i==2) {
                inputText.secureTextEntry =YES;
                
            }else if(i==0)
            {
                [inputText setKeyboardType:UIKeyboardTypePhonePad];
            }
            [inputText setPlaceholder:rightArr[i]];
            [inputText setDelegate:self];
            inputText.textAlignment=NSTextAlignmentRight;
            
            [inputText setFont:[UIFont systemFontOfSize:16]];
            [inputText setTextColor:[UIColor blackColor]];
            [inputText setFrame:CGRectMake(300*Width, 0,420*Width,106*Width)];
            [bgview addSubview:inputText];
                //横线
        UIImageView*xian =[[UIImageView alloc]init];
        xian.backgroundColor =BGColor;
        [bgview addSubview:xian];
        xian.frame =CGRectMake(0,104*Width, CXCWidth, 2*Width);
        
            bgview.frame =CGRectMake(0, 20*Width+106*Width*i, CXCWidth, 106*Width);
            
        }

    
    
    //下一步按钮
    nextBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [nextBtn setFrame:CGRectMake(40*Width,450*Width , 670*Width, 88*Width)];
    [nextBtn setBackgroundColor:NavColor];
    [nextBtn.layer setCornerRadius:4];
    [nextBtn.layer setMasksToBounds:YES];
    [nextBtn setTitle:@"注册" forState:UIControlStateNormal];
    [nextBtn.titleLabel setTextColor:[UIColor whiteColor]];
    [nextBtn.titleLabel setFont:[UIFont boldSystemFontOfSize:18]];
    [nextBtn addTarget:self action:@selector(nextStep) forControlEvents:UIControlEventTouchUpInside];
    [bgScrollView addSubview:nextBtn];
    
    
    
    
    
}

- (void)nextStep
{
    
    
    [self  registToFWQ:@""];
    
}

-(void)registToFWQ:(NSString *)upLoadPhoneStr
{
//    //判断
//    for (int i = 0; i<3; i++) {
//        //收起所有键盘
//        UITextField *inputTExt = (UITextField *)[self.view viewWithTag:i+10];
//        [inputTExt resignFirstResponder];
//            if (i==0) {
//            //判断手机号
//            if (![PublicMethod isMobileNumber:inputTExt.text ]) {
//                [MBProgressHUD showError:@"手机号格式不正确" ToView:self.view];
//                return;
//            }
//        }
//    }
    
    UITextField *accountTF = (UITextField *)[self.view viewWithTag:10];
    UITextField *inputTExtMM = (UITextField *)[self.view viewWithTag:11];
    UITextField *inputTExtQRMM = (UITextField *)[self.view viewWithTag:12];
        //判断密码是否一致
//    if (![inputTExtMM.text isEqualToString:inputTExtQRMM.text]) {
//        [MBProgressHUD showError:@"两次密码不一致" ToView:self.view];
//        return;
//    }
    
    NSMutableDictionary *dic1 = [NSMutableDictionary dictionary];
    [dic1 setDictionary:@{
                          @"account":[NSString stringWithFormat:@"%@",accountTF.text],
                          @"password":[NSString stringWithFormat:@"%@",inputTExtMM.text],
                          @"password2":[NSString stringWithFormat:@"%@",inputTExtQRMM.text],
                          }];
    NSLog(@"%@",dic1);
    
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"html/text",@"text/json", @"text/html", @"text/plain",nil];
    NSString *url=[NSString stringWithFormat:@"%@/%@",SERVERURL,@"index.php/Home/Login/regMember"];
    //    NSDictionary *dic = [PublicMethod ASCIIwithDic:dic1];//当加密的时候用
    NSMutableDictionary*parameter =[NSMutableDictionary dictionary];
    
    [parameter setDictionary:dic1];
    [manager POST:url parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
         } progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:self.view];
        
        NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"图片请求成功JSON:%@", JSON);
        if ([ [NSString stringWithFormat:@"%@",[JSON objectForKey:@"code"]]isEqualToString:@"0"])
        {
            [self.navigationController  popViewControllerAnimated:YES];

            [self performSelector:@selector(delayMethodSucess) withObject:nil afterDelay:0.5f];

        }else
        {
            [ProgressHUD dismiss];
            [MBProgressHUD showError:[NSString stringWithFormat:@"%@",[JSON objectForKey:@"msg"]] ToView:self.view];
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [MBProgressHUD hideHUDForView:self.view];
        [MBProgressHUD showError:@"加载出错" ToView:self.view];
        NSLog(@"请求失败:%@", error.description);
    }];
    
}







-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //点击其他地方收起键盘
    for (int i = 0; i<3; i++) {
        UITextField *inputTExt = (UITextField *)[self.view viewWithTag:i+10];
        [inputTExt resignFirstResponder];
    }
    
    
}
- (void)levelChoosen:(UIButton *)btn
{
    //收起键盘
    for (int i = 0; i<3; i++) {
        UITextField *inputTExt = (UITextField *)[self.view viewWithTag:i+10];
        [inputTExt resignFirstResponder];
    }
    
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSInteger existedLength = textField.text.length;
    NSInteger selectedLength = range.length;
    NSInteger replaceLength = string.length;
    if (textField.tag==10) {
        if (existedLength - selectedLength + replaceLength >= 12) {
            
            [textField resignFirstResponder];
            return NO;
            
        }
        
    }
    if (textField.tag==12||textField.tag ==11)
    {
        
        if (existedLength - selectedLength + replaceLength >= 17) {
            [MBProgressHUD showError:@"密码为6-16位" ToView:self.view];
            
            [textField resignFirstResponder];
            return NO;
            
        }
        
        
    }
    
    return YES;
    
    
    
}
- (void)delayMethodSucess
{
    [ProgressHUD showSuccess:@"注册成功"];

}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
